package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.CommentParamBO;
import com.wangdao.wechatmall.bean.pojo.Comment;
import com.wangdao.wechatmall.bean.pojo.CommentExample;
import com.wangdao.wechatmall.bean.vo.admin.BaseDataVO;
import com.wangdao.wechatmall.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 查询评论列表
 * @Author: JK
 * @Date: create in 2021/7/10 15:37
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    /**
     * 查询评论列表
     * @param param
     * @return
     */
    @Override
    public BaseDataVO<Comment> queryCommentList(CommentParamBO param) {
        // 通过PageHelper进行分页查询
        PageHelper.startPage(param.getPage(), param.getLimit());

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (!StringUtils.isEmpty(param.getUserId())) {
            criteria.andUserIdEqualTo(param.getUserId());
        }
        if (!StringUtils.isEmpty(param.getValueId())) {
            criteria.andValueIdEqualTo(param.getValueId());
        }
        criteria.andDeletedEqualTo(false);

        // 设置分页查询
        commentExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        // 在执行完分页操作之后可以获得分页信息，查询结果会放在PageInfo的构造方法当中
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();

        return BaseDataVO.ok(comments, total);
    }

    /**
     * 删除评论
     * @param comment
     * @return
     */
    @Override
    public int deleteComment(Comment comment) {
        Comment commentToDelete = new Comment();
        commentToDelete.setId(comment.getId());
        commentToDelete.setDeleted(true);
        commentToDelete.setUpdateTime(new Date());

        int affectedRows = commentMapper.updateByPrimaryKeySelective(commentToDelete);
        return affectedRows;
    }
}
